<?php
namespace WebApper\Shortcode;

/*
 * [reservationBooking]
 *
 */
 class ReservationBooking extends \WebApper\Shortcode {
	
    /**
     * Define shortcode properties
     *
     */
	protected $shortcode = 'reservationBooking';
	
    /**
     * Handles the add post shortcode
     *
     * @param array $atts
     */
	 
    public function shortcode( $atts ) {

			global $wpdb, $webapper, $current_user;			
			$serviceId 	 = $_GET['serviceId'];
			$wek_num	 = $_GET['wek_num'];
			$bookingDate = $_GET['date'];
			$serviceTable 	= $webapper->tables->services;
			$scheduleTable 	= $webapper->tables->schedules;
		    $bookingTable = $webapper->tables->booking;
			$serviceResults = $wpdb->get_results($wpdb->prepare("Select * from {$serviceTable} where service_id=%d",$serviceId), ARRAY_A);
			$timeDuration = $serviceResults[0]['service_duration'];
			$formShortCodeField    = stripcslashes($serviceResults[0]['form_shortcode']);
			$rows = $wpdb->get_results($wpdb->prepare("Select * from {$scheduleTable} where  service_id =%d and week_num=%d order by id ASC", $serviceId, $wek_num), ARRAY_A);
			$timeAvailable = "";
			$chkCount = 0;
			$availableTimeArray = array();
			foreach($rows as $row){
				$timeDiff = ((int)$row['endTime']) - ((int)$row['startTime']);
				$timeSlot = (int)((int)$timeDiff / (int)$timeDuration);
				for($i=0; $i < $timeSlot; $i++){
					$time = ((int)$row['startTime']) + (int)((int)$timeDuration * $i);
					$h = (int)($time / 60);
					$m = (int)($time % 60);					
					$tTime = $h.":".$m;
					$availableTimeArray[] = $tTime;
					$bookingTime = $wpdb->get_results($wpdb->prepare("Select reserveTime from {$bookingTable} where  service_id =%d and week_num=%d and reserveTime=%s and reserveDate=%s", $serviceId, $wek_num, $tTime, $bookingDate), ARRAY_A);
					$newDateTime = date('h:i A', strtotime($h.":".$m));	
					if(count($bookingTime) == 0){								
						$timeAvailable .='<div class="span2">
											<div class="checkbox">
												<label>
												  <input name="reserveTime" value="'.$serviceId.','.$wek_num.','.$bookingDate.','.$h.":".$m.'" type="checkbox" id="reserveTime_'.$chkCount.'" onclick="javascript:enable_disable_checkbox(this.id);">'. $newDateTime .'
												</label>
											</div>
										</div>';
						$chkCount++;
					}
				}				
			}			
			?>
            <div class="row-fluid">
            	<div class='row'><h3>Booking Available Time </h3><?php echo $timeAvailable; ?> </div>
                	<?php
						add_filter('recordform_pre_render', 'add_required_fields' );
					 	echo do_shortcode($formShortCodeField);						
					 ?>                
            </div>            
            <script type="text/javascript">			
				var boookingTimeArray = Array(); //global array declaration for storing the item
				var enable_disable_checkbox = function (chkId){
					if(document.getElementById(chkId).checked) { 
						boookingTimeArray.push(jQuery("#"+chkId).val()); // insert the value into an array
						jQuery("input:checkbox[name=reserveTime]").attr("disabled", true);	// all check box disable
						jQuery("input:checkbox[id="+chkId+"]").removeAttr("disabled"); //disable attribute remove from current checkbox
					}
					else {
						RemoveItem(boookingTimeArray , jQuery("#"+chkId).val()); // when unchecked, item remove from 
						jQuery("input:checkbox[name=reserveTime]").removeAttr("disabled"); //disable attribute remove from all checkbox
					}
					jQuery('input[name="booking-timestamp"]').val(boookingTimeArray); // set form hidden(booking-timestamp) field value			
				}
				function RemoveItem(arr, item) {
				  for(var i = arr.length; i--;) {
					  if(arr[i] === item) {
						  arr.splice(i, 1);
					  }
				  }
				}			
							
			</script>            
	<?php 
	}	
}
$initialize = new ReservationBooking(); 

?>